#include "Client.h"

Client::Client(Settings * settings, MainWindow * window, QObject *parent) : QObject(parent){
    this -> window = window;
    this -> settings = settings;

    qPcapThread = new QPcap();
    //qPcapThread->addingPortRestrictRange(5000,5500);
    qPcapThread->addingProtocolRestrict(QPcap::protocolUDP);

    this -> opCodeRecieved = new QList<unsigned short>();

    connect(this, SIGNAL(signalLog(QString, QString, QColor, QColor)), window, SLOT(slotLog(QString, QString, QColor, QColor)));
    connect(this, SIGNAL(signalLog(QString, QColor)), window, SLOT(slotLog(QString, QColor)));
    connect(this -> qPcapThread, SIGNAL(signalNewPacket(QPcapPacket*)), this, SLOT(slotGetPacket(QPcapPacket*)));
    connect(this, SIGNAL(signalSetStatusBar(QString,int)), window, SLOT(slotSetStatusBar(QString,int)));
}


Client::~Client(){
    delete this -> qPcapThread;
    delete this -> opCodeRecieved;
}


QPcap * Client::getQpcapThread(){
    return this -> qPcapThread;
}

void Client::stop(){
    this -> qPcapThread -> stop();
}


void Client::start(){
   qPcapThread -> selectInterface(1 + qPcapThread -> getIndexFromInterfaceName(this -> settings -> getValue("selectedInterface").toString()));
   qPcapThread -> setRefreshInterval(settings -> getValue("readTimeout").toInt());
   qPcapThread -> start();
   emit signalSetStatusBar(QString("Capturing on ") + this -> qPcapThread -> getActiveInterfaceName());
}



void Client::slotGetPacket(QPcapPacket * packet){
    usePacket(packet);
    delete packet;
}



void Client::usePacket(QPcapPacket * packet){
    /*
    if (packet->getDestinationPort() < 5000 || packet->getDestinationPort() > 5500)
        return;

    QList<int> restrict = QList<int>();
    restrict << 41 << 30 << 99 << 29 << 12 << 14 << 23 << 24 << 51 << 48 << 66 << 43 << 31 << 52 << 22 << 63 << 54 << 42 << 59 << 80 << 53 << 38 << 98 << 45 << 60 << 44 << 47;

    if (restrict.contains(packet->getData()->size()))
        return;

    if (packet->getData()->size() == 0)
        qDebug() << "You killed the blue !";

    if (packet->getData()->size() == 77 )
        qDebug() << "Wolves spawned !";
      */
    emit signalLog(QString("Packet Received"), packet -> toString());
}




